import java.util.ArrayList;
import java.util.List;

/**
 * @author huu
 * @create 2025-09-08-14:38
 */
public class partitionLabels {
    //合并区间
    public List<Integer> partitionLabels(String S) {
        char[] s = S.toCharArray();
        int[] a = new int[26];//26个字母
        for (int i = 0; i < s.length; i++) {
            a[s[i] - 'a'] = i;
        }//记录每个字母出现的最后位置
        int start = 0, end = 0;
        List<Integer> res = new ArrayList<>();
        for(int i = 0; i < s.length; i++){
            end = Math.max(end, a[s[i]-'a']);
            if(i == end){
                //找到了一个区间
                res.add(end-start+1);
                start = i+1;
            }
        }
        return res;

    }
}
